export interface StorySegment{
    text:string;
    startParagraph:number;
    endParagraph:number;
    summary:string;
    imgs:Object;
    name:string;
}
let segmentEvery = 1000
export class Story{
    paragraphs:string[] = [];
    segments:StorySegment[] =[]

    constructor(text:string){
        this.paragraphs = text.split("\n")
        let count = 0;
        let content:string = ""
        let start = -1
        for(let i=0;i<this.paragraphs.length;i++){
            if(start === -1) start = i;
            count+= this.paragraphs[i].length
            content+=this.paragraphs[i]
            if(count>=segmentEvery || (i<this.paragraphs.length-1 && this.paragraphs[i+1].match('第.*[章节]')!==null && this.segments.length!==0)){
                this.segments.push({
                    text:content,
                    startParagraph:start,
                    endParagraph:i,
                    name:`${start}-${i}`,
                    summary:"",
                    imgs: []
                })
                start = -1;
                count = 0;
                content = ""
            }
        }
        if(count>0){
            this.segments.push({
                text:content,
                startParagraph:start,
                endParagraph:this.paragraphs.length-1,
                summary:"",
                name:`${start}-${this.paragraphs.length-1}`,
                imgs: []
            })
            start = -1;
            count = 0;
            content = ""
        }
    }
    render():string{
        let html = `<style>${this.getCss()}</style>`

        for(let i=0;i<this.segments.length;i++){
            for(let j=this.segments[i].startParagraph;j<=this.segments[i].endParagraph;j++){
                if(this.paragraphs[j].match('第.*[章节]')!==null){
                    html+=`<h1>${this.paragraphs[j]}</h1>`
                    continue
                }
                html+="<p>"+this.paragraphs[j]+"</p>"
            }
            html+="<br/>"
            for(let imgurl in this.segments[i].imgs){
                //@ts-ignore
                console.log(this.segments[i].imgs)
                //@ts-ignore
                html+=`<img src="${this.segments[i].imgs[imgurl]}"/>`
            }
        }
        return html
    }
    getCss():string{
        return  "p:before{\n" +
                "    content: \"\";\n" +
                "    display: inline-block;\n" +
                "    width: 2em;\n" +
                "}" +
                "h1 {" +
                "text-align: center;}"
    }
}

